package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.DateUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.zsmart.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;
import java.util.Objects;
/**
 * SDH-专线-AZ端要求完成时间校验
 */

public class LocalZxAzRequiredCompleteTimeHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.before(attr, inst, value, data);

        //重提初始化不校验
        //fixme 可能导致频繁访问缓存,上线临时处理方案
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (Objects.nonNull(extParams)&& extParams.containsKey("nodeCode")) {
            return;
        }

        // 变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(
            querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))
            || KiteStringUtils.equals(attr.getOldValue(), value)) {
            return;
        }

        if (KiteStringUtils.isEmpty(value)){
            return;
        }

        String allCompleteTimePattern = "yyyy-MM-dd HH:mm:ss";
        String azCompleteTimePattern = "yyyy-MM-dd";

        Date complateDate = null;
        try{
            complateDate = DateUtils.parseStrToDate(value, azCompleteTimePattern);
        }catch (Exception e){
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("日期转换异常");
        }

        //全程要求完成时间
        Date allCompleteDate = null;
        String allCompleteTimeStr = querySceneInstService.getCommonAttrValue(data.getSceneInstId(), "ORD10012");
        if(KiteStringUtils.isNotEmpty(allCompleteTimeStr)){
            try{
                allCompleteDate = DateUtils.parseStrToDate(allCompleteTimeStr, allCompleteTimePattern);
            }catch(Exception e){
                ErrorConsts.BUSI_COMMON_ERROR.throwOut("日期转换异常");
            }

            //A、Z端要求完成时间 不能晚于全程要求完成时间
            if (null != complateDate && DateUtil.compare(complateDate,allCompleteDate) >1){
                BssException.throwOut(KeyConsts.LOG_MODULE, "", "A、Z端要求完成时间不能晚于全程要求完成时间");
            }
        }
    }

    public void def(KiteAttr attr, KiteInst inst, AttrData data) {
        String defaultValue = DateFormatUtils.businessDaysFrom(new Date(), 3);
        attr.setDefaultValue(defaultValue);
        super.def(attr, inst, data);
    }
}
